class GameScene extends egret.DisplayObjectContainer {
	public constructor() {
		super();
		
		this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
	}
	private stageW: number;
	private stageH: number;
	private player: Player;
	private leaf: leaf;
	 
	private map: StoneMap;
	private canPassStone: Stone[];
	private stoneDownTimer: egret.Timer;
	private stepIndex:number = 0;

	private onAddToStage() {

		this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

		this.stageW = this.stage.stageWidth;
		this.stageH = this.stage.stageHeight;

		let mapr: StoneMap = new StoneMap();
		this.map = mapr;
		this.addChild(this.map);

		let palyer: Player = new Player();
		this.player = palyer;
		this.addChild(this.player);
		this.player.x = this.stageW / 2;
		this.player.y = this.map.getCanPassStone()[0].y;
		
		this.player.playerDirection(this.map.getCanPassStoneDis()[1]);

		this.stage.addEventListener(egret.TouchEvent.TOUCH_TAP, this.playerJump, this);
		this.stoneDownTimer = new egret.Timer(500,0);
		this.stoneDownTimer.addEventListener(egret.TimerEvent.TIMER, this.autoDown, this);
	
		mapr.addEventListener("gameOver",()=>{
			this.jumpDied();
		},this)
	}
	
	private playerJump(e: egret.TouchEvent) {
		this.stepIndex++;
		if (!this.stoneDownTimer.running&&this.stepIndex>8){
			this.stoneDownTimer.start();
		}	
		let canPassStoneDisArr = this.map.getCanPassStoneDisArr();
		this.canPassStone = this.map.getCanPassStone();
		let downStone: Stone = canPassStoneDisArr[this.stepIndex-1].stone;
		let touchX: number = e.stageX;
		if (touchX <= this.stageW / 2) {
			this.player.playerJump(0);
			if (canPassStoneDisArr[this.stepIndex].dis == 0) {
				delete(canPassStoneDisArr[this.stepIndex-2]);
			
				this.jumpRight();
			} else {
				downStone.stoneDown();
				this.jumpDied();
			}
		}
		if (touchX > this.stageW / 2) {
			this.player.playerJump(1);
			if (canPassStoneDisArr[this.stepIndex].dis == 1) {
				delete(canPassStoneDisArr[this.stepIndex-2]);
				this.jumpRight();
			} else {
				downStone.stoneDown();
				this.jumpDied();
			}
		}
		
		 	egret.Tween.get(downStone).to({y: downStone.y+100 }, 150).to({y: downStone.y})
		
	}
	private jumpRight(){
		this.map.mapMove();
	}

	public jumpDied() {
		this.stage.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.playerJump, this);
		this.stoneDownTimer.removeEventListener(egret.TimerEvent.TIMER, this.autoDown, this);
		this.player.playerDied(0);
		egret.Tween.removeTweens(this);
		console.log("player died!");
		this.map.destroy();
	}

	// private timeOver(e: egret.TimerEvent) {
	// 	this.stage.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.playerJump, this);
	// 	this.stoneDownTimer.removeEventListener(egret.TimerEvent.TIMER, this.timeOver, this);
	// 	this.stoneDownTimer.removeEventListener(egret.TimerEvent.TIMER, this.autoDown, this);
	// 	console.log("time is over !");
		
	// 	this.canPassStone = this.map.getCanPassStone();
	// 	let downStone: Stone = this.canPassStone[0];
	// 	downStone.stoneDown();
	// 	this.player.playerDied(1);
	// }
	private autoDown(event:egret.TimerEvent){
		let downNum = this.map.getDownCanPassStone();
		if (downNum===this.stepIndex){
			this.jumpDied();
			return;
		}
		this.map.AutoRemoveStoneAndDis();
	}

}